﻿using System;
using System.Collections.Generic;
using CzfSDK;
using MySql.Data.MySqlClient;
using System.Data;
using System.Collections;

namespace Server
{
	public static class RoleManager
	{
		public static List<int> GetRoleIds(int userId)
		{
			return GetAll (userId);
		}

		// 购买角色
		public static string BuyRole(int userId, int roleId)
		{
			TryAddRole (userId, roleId);
			var has = Get (userId, roleId);
			if (has != null) 
				throw new CzfProtoException (Constants.ERR3, "不能重复购买");			

			var config = MConfig.GetRoleConfig (roleId);
			if(config == null)
				throw new CzfProtoException (Constants.ERR3, "不存在的角色");

			var price = config.GetVal<int> ("price");

			// 扣钱
			BagManager.DelItem(config.GetVal<int> ("pricetype") + "," + price, userId, "购买角色", roleId.ToString());

			// 发奖励
			BagManager.AddItem(roleId + ",1", userId, "购买角色", roleId.ToString());

			return "ok";
		}

		#region 给玩家增加角色
		public static void TryAddRole(int userId, int roleId)
		{
			if (!MConfig.GetRoleTemplate ().ContainsKey (roleId))
				throw new CzfProtoException (Constants.ERR3, "不存在的角色ID:" + roleId);
		}

		// 给玩家增加角色
		public static string AddRole(int userId, int roleId)
		{
			var has = Get (userId, roleId);
			if (has != null) {
				// 已经有角色了,转化为别的资源
				string prize = BagManager.COIN_ID + ",100";
				BagManager.AddItem(prize, userId, "多余角色转化", roleId.ToString());
				return prize;
			}

			Create (userId, roleId);

			// 自动获得角色大招
			string bigSkillId = "";
			foreach (var skillConfig in MConfig.GetSkillTemplate().Values) {
				if (skillConfig.GetVal<int> ("ascription") == roleId && skillConfig.GetVal<int> ("category") == 3) {					
					bigSkillId = skillConfig.GetVal<int> ("id").ToString();
					BagManager.AddItem (bigSkillId + ",1", userId, "获得角色自动获得大招", roleId.ToString());
				}
			}

			// 自动创建三套阵容
			string initSkill = MConfig.GetGameConfig<string>("initSkill") + "," + bigSkillId;
			FormationManager.CreateFormation(userId, "阵容I", initSkill, roleId);
			FormationManager.CreateFormation(userId, "阵容II", initSkill, roleId);
			FormationManager.CreateFormation(userId, "阵容III", initSkill, roleId);

			return roleId + ",1";
		}
		#endregion

		#region DB
		// 查询玩家的某个角色
		public static Hashtable Get(int userId, int roleId)
		{
			var sql = "select * from role where userid = @userid and roleid = @roleid";
			var dr = MysqlPool.current.ExecDataRow (sql,
				new MySqlParameter ("@userid", userId),
				new MySqlParameter ("@roleid", roleId)
			);
			if (dr == null)
				return null;
			else
				return dr.ToHashtable ();
		}

		// 查询某玩家的角色列表
		public static List<int> GetAll(int userId)
		{
			var sql = "select * from role where userid = @userid";
			var dt = MysqlPool.current.ExecDataTable (sql,
				new MySqlParameter("@userid", userId)
			);

			var ret = new List<int> ();
			if (dt != null) {
				foreach (DataRow dr in dt.Rows) {
					ret.Add (Convert.ToInt32 (dr ["roleid"]));
				}
			}
			return ret;
		}

		// 给玩家增加一个角色
		private static int Create(int userId, int roleId)
		{
			var sql = "insert into role(userid, roleid, createtime) values(@userid, @roleid, now())";
			return MysqlPool.current.ExecuteNonQuery (sql, 
				new MySqlParameter ("@userid", userId),
				new MySqlParameter ("@roleid", roleId)
			);
		}
		#endregion
	}
}

